package ReviewAlgorithms.sort;

import java.util.PriorityQueue;
import java.util.Queue;

public class LastKthNum {

    public static void main(String[] args) {
        int[] nums = {3,2,3,1,2,4,5,5,6};
        int k = 4;
        System.out.println(findKthLargest(nums,k));
    }

    public static int findKthLargest(int[] nums, int k) {
        if (k > nums.length){
            return -1;
        }
        if (nums.length < 1){
            return -1;
        }
        Queue<Integer> queue = new PriorityQueue<>((o1,o2) -> o2 - o1);
        for (int i = 0; i < nums.length; i++) {
            queue.add(nums[i]);
        }

        for (int i = 0; i < k - 1; i++) {
            queue.poll();
        }
        return queue.poll();
    }
}
